package com.eastjet.maintenance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.maintenance.domain.DispositionDetails;
import com.eastjet.maintenance.domain.EquipmentDisposal;
import com.eastjet.maintenance.feign.CodeDataFeing;
import com.eastjet.maintenance.mapper.EquipmentDisposalMapper;
import com.eastjet.maintenance.service.EquipmentDisposalService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 杨博文
 * @description 针对表【equipment_disposal(设备处置)】的数据库操作Service实现
 * @createDate 2024-11-25 09:23:38
 */
@Service
public class EquipmentDisposalServiceImpl extends ServiceImpl<EquipmentDisposalMapper, EquipmentDisposal> implements EquipmentDisposalService {

    @Autowired
    private DispositionDetailsServiceImpl dispositionDetailsServiceImpl;//设备处置详情Service
    @Autowired
    private EquipmentDisposalMapper equipmentDisposalMapper;//设备处置Mapper
    @Autowired
    private CodeDataFeing codeDataFeing;//自动生成编码服务

    /*
    新增设备处置
     */
    @SneakyThrows
    @Override
    public Boolean InsertEquipmentDisposal(Map<String, Object> map) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String code = codeDataFeing.test(new HashMap<String, String>() {{
            put("TableName", "equipment_disposal");
        }});
        EquipmentDisposal p = new EquipmentDisposal();
        p.setCode(code);
        p.setState(0);
        p.setTopic(map.get("topic").toString());
        p.setApplicant(map.get("applicant").toString());
        p.setDepartment(map.get("department").toString());
        p.setDisposal(map.get("disposal").toString());
        p.setDate(df.parse(map.get("date").toString()));
        p.setCause(map.get("cause").toString());
        p.setRemark(map.get("remark").toString());
        p.setCreatedBy(map.get("applicant").toString());
        p.setCreationTime(df.parse(df.format(new Date())));
        p.setIs_delete(0);
        boolean save = this.save(p);
        //新增处理明细
        if (save) {
            List<Map<String, Object>> tableData = (List<Map<String, Object>>) map.get("tableData");
            List<DispositionDetails> list = new ArrayList<>();
            //映射处理明细对象
            tableData.forEach(item -> {
                item = (Map<String, Object>) item;
                DispositionDetails d = new DispositionDetails();
                d.setCode(code);
                d.setDeviceNumber(((Map<?, ?>) item).get("code").toString());
                d.setName(((Map<?, ?>) item).get("name").toString());
                d.setSpecification(((Map<?, ?>) item).get("specifications").toString());
                d.setDeviceType(((Map<?, ?>) item).get("deviceType").toString());
                d.setBrand(((Map<?, ?>) item).get("brand").toString());
                d.setDatePurchase(((Map<?, ?>) item).get("dateofManufacture").toString());
                d.setHead(((Map<?, ?>) item).get("theEquipment").toString());
                d.setStorageLocation(((Map<?, ?>) item).get("storageLocation").toString());
                d.setPurchasePrice(BigDecimal.valueOf(Double.parseDouble(((Map<?, ?>) item).get("originalValue").toString())));
                d.setUseDepartments(((Map<?, ?>) item).get("useDepartments").toString());
                d.setDisposalLocation(((Map<?, ?>) item).get("newLocation").toString());
                d.setRemark(((Map<?, ?>) item).get("note").toString());
                d.setCreatedBy(((Map<?, ?>) item).get("theEquipment").toString());
                try {
                    d.setCreationTime(df.parse(df.format(new Date())));
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                d.setTombstone(0);
                list.add(d);
            });
            return dispositionDetailsServiceImpl.InsertDispositionDetails(list);
        }
        return null;
    }

    /*
    修改设备处置
     */
    @Override
    public Boolean UpdateEquipmentDisposal(List<EquipmentDisposal> equipmentDisposal) {
        return this.updateBatchById(equipmentDisposal);
    }

    /*
    删除设备处置
     */
    @Override
    public Boolean DeleteEquipmentDisposal(List<EquipmentDisposal> equipmentDisposal) {
        return this.removeByIds(equipmentDisposal);
    }

    /*
    查询设备处置
     */
    @Override
    public List<EquipmentDisposal> SelectEquipmentDisposal(EquipmentDisposal equipmentDisposal) {
        return equipmentDisposalMapper.SelectEquipmentDisposal(equipmentDisposal);
    }
}




